#include "tests.h"
#include "rss.h"
#include "ini.h"
#include "utilities.h"

#include <stdio.h>

//------------------------------------------------------------------------------
// test xml file
//------------------------------------------------------------------------------

#define TEST_RSS_FILE             "rss_test.xml"

//------------------------------------------------------------------------------
// test customer
//------------------------------------------------------------------------------

customer_t s_test_customer = {0};

//------------------------------------------------------------------------------
// test functions
//------------------------------------------------------------------------------

int init_test_suite_rss(void)
{
    s_test_customer.name = "Oguz";
    s_test_customer.surname = "Kupusoglu";
    s_test_customer.email = "oguz.kupusoglu@gmail.com";
    s_test_customer.time_zone = "GMT+4";

    // keywords
    s_test_customer.keywords = (char**) malloc(sizeof(char*) * 6);
    s_test_customer.keywords[0] = "Italy";
    s_test_customer.keywords[1] = "Georgia";
    s_test_customer.keywords[2] = "Russia";
    s_test_customer.keywords[3] = "Czech";
    s_test_customer.keywords[4] = "France";
    s_test_customer.keywords[5] = NULL;

    // use the default ini path!
    return (INI_Init(0) && RSS_Init(NULL, 1, &s_test_customer, 1) ? 0 : 1);
}

int cleanup_test_suite_rss(void)
{
	INI_Free();
    RSS_Free();

    return 0;
}

void test_rss_download()
{
	char rss_path[MAX_PATH_LENGTH] = {0};
    bool result = false;
    const feed_t* feeds = NULL;
    int j;

    // load a saved rss feed
	_snprintf(rss_path, MAX_PATH_LENGTH - 1, "%s\\%s", INI_GetDataPath(), TEST_RSS_FILE);
    result = RSS_LoadFeed(rss_path);

    CU_ASSERT_EQUAL(result, true);

    // do something with the feed
    feeds = RSS_GetFeeds();

    printf("\n\nfeed\n"
            "    title    :|%s|\n"
            "    pubdate  :|%s|\n"
            "    language :|%s|\n"
            "    itemcount:|%d|\n\n",
            feeds[0].title,
            feeds[0].pubdate,
            feeds[0].language,
            feeds[0].itemcount);

    for (j = 0; j < feeds[0].itemcount; j++)
    {
        printf("    item |%d|:\n"
                "        title      :|%s|\n"
                "        link       :|%s|\n"
                "        description:|%s|\n"
                "        pubdate    :|%s|\n\n",
                j,
                feeds[0].items[j].title,
                feeds[0].items[j].url,
                feeds[0].items[j].description,
                feeds[0].items[j].pubdate);
    }
}

void test_rss_create_digest()
{
    bool res = RSS_CreateDigests();
	int keyword;
	int item;

    CU_ASSERT_EQUAL(res, true);

    printf("\n\n\n\ndigest:\n");

    for (keyword = 0; s_test_customer.keywords[keyword] != NULL; keyword++)
    { 
        printf("    test_customer:|%s %s|  keyword[%d]:|%s|\n",
                s_test_customer.name,
                s_test_customer.surname,
                keyword,
                s_test_customer.keywords[keyword]);

		if (s_test_customer.raw_digest[keyword] == NULL)
		{
			printf("        no item found!\n\n");
		}
		else
		{
			// first count the digest items for the customer
			for (item = 0; s_test_customer.raw_digest[keyword][item] != NULL; item++)
				;

			printf("        |%d| item%s found\n\n", item, (item > 1 ? "s" : ""));

			// next dump the digest items one-by-one
			for (item = 0; s_test_customer.raw_digest[keyword][item] != NULL; item++)
			{
				printf("            digest item[%d]\n"
                        "                 title:|%s|\n"
                        "                  desc:|%s|\n"
						"                  link:|%s|\n"
						"               pubdate:|%s|\n\n", 
						item,
						s_test_customer.raw_digest[keyword][item]->title,
						s_test_customer.raw_digest[keyword][item]->description,
						s_test_customer.raw_digest[keyword][item]->url,
						s_test_customer.raw_digest[keyword][item]->pubdate);
			} // for item
		}
	} // for keyword
}

void add_test_suite_rss()
{
    CU_pSuite pSuite = NULL;

    // add a suite to the registry
    pSuite = CU_add_suite("RSS test suite", init_test_suite_rss, cleanup_test_suite_rss);
    if (NULL == pSuite)
    {
        print_last_error();
        return;
    }

    // add the tests to the suite
    if ((NULL == CU_ADD_TEST(pSuite, test_rss_download))
        ||
        (NULL == CU_ADD_TEST(pSuite, test_rss_create_digest)))
    {
        print_last_error();
    }
}
